﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace U8
{
    public class Inventory_gx
    {
        string strConn = ConfigurationManager.ConnectionStrings["conn1"].ConnectionString;
        string UserId = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
        public Inventory_gx() 
        {
           // this.strConn = strConn;
          //  this.UserId = UserId;
        }




        /// <summary>
        /// 保存存货档案
        /// </summary>
        /// <param name="dsData"></param>
        /// <param name="bom"></param>
        /// <returns></returns>
        public string Save(InventoryInfo_GX bom)
        {
           
            SqlConnection Conn = new SqlConnection(strConn);
            Conn.Open();
            SqlTransaction tran = Conn.BeginTransaction();
            try
            {
                SaveBom( bom, UserId, Conn, tran);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return ex.Message;
            }
            return "";
        }

        /// <summary>
        /// 保存BOM信息
        /// </summary>
        /// <param name="dtPart"></param>
        /// <param name="bom"></param>
        /// <param name="Conn"></param>
        /// <param name="Tran"></param>
        public void SaveBom(InventoryInfo_GX bom, string DBName, SqlConnection Conn, SqlTransaction Tran)
        {
            DataSet ds = GetBOMModel();
            long BomId = 0L;
            GetVoucherID(DBName.Substring(7, 3), "sfc_operation", ref BomId);
            {
                DataRow dr = ds.Tables["sfc_operation"].NewRow();
                dr["DeliveryDays"] = 0;
                dr["PlanSubFlag"] = 0;
                dr["FeeFlag"] = 1;
                dr["BFFlag"] = 0;
                dr["ReportFlag"] = 1;
                dr["VTid"] = 30376;
                dr["SubFlag"] =0;
                dr["RltOptionFlag"] = 0;
                dr["SubFlag"] = 0;
                dr["RltOptionFlag"] = 0;
                dr["OperationId"] = BomId;
                dr["OpCode"] = bom.OpCode;
                dr["Description"] = bom.Description;
                ds.Tables["sfc_operation"].Rows.Add(dr);
            }

          

            //保存数据
            foreach (DataTable dt in ds.Tables)
            {
                SqlHelper.SaveTable(dt, Conn, Tran, null, DBName);
            }
        }



        public DataSet GetBOMModel()
        {
            DataSet ds = new DataSet();
            string[] tbls = "sfc_operation".Split(',');
            foreach (string tbl in tbls)
            {
                try
                {
                    string sql = string.Format("Select * from {0} With(nolock) Where 1=2 ", tbl);
                    DataTable dt = SqlHelper.Query(sql, strConn);
                    dt.TableName = tbl;
                    ds.Tables.Add(dt);
                }
                catch (Exception ex)
                {
                }
            }
            return ds;
        }



      
       

 
    

        public bool GetVoucherID(string accId, string VouchType, ref long FatherID)
        {
            string strErr = "";
            long id = 0L;
            return GetVoucherID(strConn, accId, VouchType, 1, ref id, ref FatherID, ref strErr);
        }

        public bool GetVoucherID(string strConn, string accId, string VouchType, int Rows, ref long FatherID, ref long ChildID, ref string errMsg)
        {
            Boolean result = false;
            try
            {
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();
                    SqlCommand GetID = conn.CreateCommand();
                    GetID.CommandType = CommandType.StoredProcedure;
                    GetID.CommandText = "sp_GetID";
                    GetID.Parameters.Add("@RemoteId", SqlDbType.VarChar, 2, "00");
                    GetID.Parameters.Add("@cAcc_Id", SqlDbType.VarChar, 3).Value = accId;
                    GetID.Parameters.Add("@cVouchType", SqlDbType.VarChar, 50).Value = VouchType;
                    GetID.Parameters.Add("@iAmount", SqlDbType.Int, 1).Value = Rows.ToString();
                    GetID.Parameters.Add("@iFatherId", SqlDbType.Int);
                    GetID.Parameters.Add("@iChildId", SqlDbType.Int);
                    GetID.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
                    GetID.Parameters["@iChildId"].Direction = ParameterDirection.Output;
                    GetID.ExecuteNonQuery();
                    FatherID = Int64.Parse(GetID.Parameters["@iFatherId"].Value.ToString());
                    ChildID = Int64.Parse(GetID.Parameters["@iChildId"].Value.ToString()) - Rows;
                    GetID.Dispose();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            return result;
        }
    }


    public class InventoryInfo_GX
    {
        public string OpCode { get; set; }
        public string Description { get; set; }  //版本号

        public string objectName { get; set; }  //版本号
    }



}








